package com.zq.learn.springbatch.combination.quartz;

import org.quartz.Scheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import static org.quartz.CronScheduleBuilder.cronSchedule;


/**
 * QuartzConfig
 *
 * @author ZhangQi
 * @date 2020/2/18
 */
@Configuration
public class QuartzConfig {
  private Logger logger = LoggerFactory.getLogger(QuartzConfig.class);

  /**
   * QuartzAutoConfiguration中载入的任务创建工厂，
   */
  @Autowired private SchedulerFactoryBean quartzScheduler;

  /**
   * 获取任务容器Scheduler（application.yml中配置的testQuartz容器
   *
   * @return
   */
  @Bean public Scheduler scheduler() {
    Scheduler scheduler = quartzScheduler.getScheduler();
    //    try {
    //      scheduler.start();//直接调用start()，则延时失效
    //    } catch (SchedulerException e) {
    //      e.printStackTrace();
    //    }
    return scheduler;
  }

  /**
   * 创建任务管理类
   *
   * @param mongoTemplate mongo操作模板
   * @param scheduler     任务容器
   * @return
   */
  @Bean public QuartzTasks quartzTasks(MongoTemplate mongoTemplate, Scheduler scheduler) {
    QuartzTasks quartzTasks = new QuartzTasks(mongoTemplate, scheduler);
    //创建定时任务，并载入任务容器
    quartzTasks.statisticOrder();
    return quartzTasks;
  }
}
